package com.leoman.plz_p.views;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.leoman.plz_p.R;
import com.leoman.plz_p.weidget.NavBar;

import butterknife.Bind;
import cn.com.corelib.base.BaseActivity;
import cn.com.corelib.base.BasePresenter;

/**
 * 自定的用于显示网页的Activity, Intent中需要传入title与url
 */
public class WebActivity extends BaseActivity {

    public static final String EXTRA_TITLE = "EXTRA_TITLE";
    public static final String EXTRA_URL = "EXTRA_URL";
    public static final String EXTRA_HTML = "EXTRA_HTML";
    public static final String EXTRA_OVERRIDE_TITLE = "EXTRA_OVERRIDE_TITLE";
    public static final String EXTRA_IS_LAND = "EXTRA_IS_LAND";

    @Bind(R.id.progress) ProgressBar progress;
    @Bind(R.id.web) WebView web;
    @Bind(R.id.nav)
    NavBar nav;

    private boolean isOverrideTitle = true;

    /**
     * @param title 标题
     * @param url 需加载的网址, 如html不为空, 则忽略此url
     * @param html 需加载的html片段, 此值不为空则不会加载指定的url
     * @param isOverrideTitle 是否将网页的标题显示到标题栏
     */
    public static Intent getLaunchIntent(Context context, String title, String url, String html,
                                         boolean isOverrideTitle,boolean isLand) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra(EXTRA_TITLE, title);
        intent.putExtra(EXTRA_URL, url);
        intent.putExtra(EXTRA_HTML, html);
        intent.putExtra(EXTRA_OVERRIDE_TITLE, isOverrideTitle);
        intent.putExtra(EXTRA_IS_LAND,isLand);
        return intent;
    }

    private WebViewClient mClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }
    };

    private WebChromeClient mChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            setNewProgress(newProgress);
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            if (isOverrideTitle) {
                nav.setTitle(title);
            }
            super.onReceivedTitle(view, title);

        }
    };


    @Override
    protected int getLayoutId() {
        return R.layout.ac_web;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        boolean isLand=getIntent().getBooleanExtra(EXTRA_IS_LAND,false);
        if (isLand){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        nav.showBack();

        String html = getIntent().getStringExtra(EXTRA_HTML);
        String title = getIntent().getStringExtra(EXTRA_TITLE);
        if (title != null && title.length() > 0) {
            nav.setTitle(title);
        }

        web.getSettings().setJavaScriptEnabled(true);
        web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        web.getSettings().setSupportMultipleWindows(true);
        web.getSettings().setAppCacheEnabled(false);
        web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        web.getSettings().setDatabaseEnabled(true);
        web.getSettings().setDomStorageEnabled(true);
        web.requestFocus();
        web.setWebViewClient(mClient);
        web.setWebChromeClient(mChromeClient);

        if (html == null || html.length() <= 0) {
            web.loadUrl(getIntent().getStringExtra(EXTRA_URL));
        } else {
            html = html.replace("<img", "<img style='max-width:100%;height:auto;'");
            web.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
        }

        isOverrideTitle = getIntent().getBooleanExtra(EXTRA_OVERRIDE_TITLE, false);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }


    private void setNewProgress(int newProgress) {
        progress.setVisibility(View.VISIBLE);
        progress.setProgress(newProgress);
        if (newProgress >= 100)
            progress.setVisibility(View.GONE);
    }

    public boolean onKeyDown(int keyCoder, KeyEvent event) {
        if (web.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK) {
            web.goBack(); // goBack()表示返回webView的上一页面
            return true;
        } else {
            finish();
        }
        return false;
    }
}
